<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
            
            <el-col :lg="6" :md="12">
            <el-form-item label="学校名称:">
              <el-input
                clearable
                v-model="where.school_name"
                placeholder="请输入学校名称"/>
            </el-form-item>
          </el-col>
            
                <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
               <el-button
                size="small"
                type="success"
                class="ele-btn-icon"
                icon="el-icon-download"
                @click="exportData">导出
              </el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="detail(row)">详情
          </el-link>
        </template>
                          
                                        
                                        
                                        
                                        
                                        
                                        
                        </ele-pro-table>
    </el-card>
    
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import XLSX from 'xlsx';

export default {
  name: 'ExamExperienceStatistics',
  components: {},
  computed: {
    ...mapGetters(["permission"]),
  },
  created() {
    this.where.experience_id = this.$route.query.experience_id;
    this.where.experience_name = this.$route.query.experience_name;
    console.log(this.where);
    this.reload();
    
  },
  data() {
    return {
      // 表格数据接口
      url: '/experiencescan/statistics',
      // 表格列配置
      columns: [
        {
          columnKey: 'index',
          type: 'index',
          width: 45,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'experience_name',
          label: '体验名称',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'school_name',
          label: '学校名称',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'project_count',
          label: '参与项目数',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'scan_sum',
          label: '扫码次数',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 详情 */
    detail(row) {
      this.$router.push({path:'/experience/detail',query:{school_id: row.school_id,school_name: row.school_name,experience_id:this.where.experience_id}}); 
    },
    /* 导出数据 */
    exportData() {
      let array = [['体验名称', '学校名称', '参与项目数', '扫码次数']];
      const loading = this.$loading({lock: true});
      this.$http.post('/experiencescan/statistics?page=1&limit=2000',this.where).then(res => {
        loading.close();
        if (res.data.code === 0) {
          res.data.data.forEach(d => {
            array.push([
              d.experience_name,
              d.school_name,
              d.project_count,
              d.scan_sum
            ]);
          });
          this.$util.exportSheet(XLSX, array, '体验报表');
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    }
    
  },
  watch: {
    $route() {
        this.where.experience_id = this.$route.query.experience_id;
        this.where.experience_name = this.$route.query.experience_name;
        console.log(this.where);
        this.reload();
    }
  }
}
</script>

<style scoped>
</style>
